<template>
  <div :class="className" :style="{height,width}" />
</template>

<script>
  import {getToken}  from "@/utils/auth";
  import echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from './mixins/resize'

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '300px'
      }
    },
    data() {
      return {
        chart: null,
        userWechat:0,
        user :0,
      }
    },
    mounted() {
      this.getUserData()
      setTimeout(() => {
        this.initChart()
      }, 1500);
      
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      async getUserData(){
        const{data:{userWechat,user}} = await this.$API.data.getUserData2(getToken());
        this.userWechat = userWechat
        this.user = user
        console.log(userWechat);
      },
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')

        const option = {
          title: {
            text: '双站点当日用户注册量',
            // textStyle:"#333",
            // subtext: '纯属虚构',
            left: 'right'
          },
          tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
          },
          legend: {
            orient: 'vertical',
            left: 'left',
            data: ['微信端', '网页端']
          },
          series: [{
            name: '访问来源',
            type: 'pie',
            radius: '55%',
            center: ['50%', '60%'],
            data: [{
                value:this.userWechat,
                name: '微信端'
              },
              {
                value: this.user,
                name: '网页端'
              },
              // {
              //   value: 234,
              //   name: '联盟广告'
              // },
              // {
              //   value: 135,
              //   name: '视频广告'
              // },
              // {
              //   value: 1548,
              //   name: '搜索引擎'
              // }
            ],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }]
        }

        // const option2 = {
        //   tooltip: {
        //     trigger: 'item',
        //     formatter: '{a} <br/>{b} : {c} ({d}%)'
        //   },
        //   legend: {
        //     left: 'center',
        //     bottom: '10',
        //     data: ['Industries', 'Technology', 'Forex', 'Gold', 'Forecasts']
        //   },
        //   series: [{
        //     name: 'WEEKLY WRITE ARTICLES',
        //     type: 'pie',
        //     roseType: 'radius',
        //     radius: [15, 95],
        //     center: ['50%', '38%'],
        //     data: [{
        //         value: 320,
        //         name: 'Industries'
        //       },
        //       {
        //         value: 240,
        //         name: 'Technology'
        //       },
        //       {
        //         value: 149,
        //         name: 'Forex'
        //       },
        //       {
        //         value: 100,
        //         name: 'Gold'
        //       },
        //       {
        //         value: 59,
        //         name: 'Forecasts'
        //       }
        //     ],
        //     animationEasing: 'cubicInOut',
        //     animationDuration: 2600
        //   }]
        // }

        this.chart.setOption(option)
      }
    }
  }

</script>
